package cn.dglydrpy.study.j2ee.servlet.day06;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 查询并在浏览器显示Cookie
 */
public class FindCookieServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter out =  response.getWriter();
		//读取浏览器发送过来的cookie
		Cookie[] cookies = request.getCookies();//有可能返回null
		if(cookies!=null){//判断一下，避免空指针异常
			for (Cookie c : cookies) {
				out.println(URLDecoder.decode(c.getName(), "utf-8") +": "+URLDecoder.decode(c.getValue(), "utf-8")+"<br>");
			}
		}else {
			out.println("没有cookie");//默认情况下cookie存放于浏览器内存中，浏览器即使之前得到cookie，但若关闭浏览器，再打开会没了！
		}
	}
}
